#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <sys/wait.h>

using namespace std;
int main()
{
    //先创建管道
    int pipnum[2]={0};
    int n=pipe(pipnum);
    (void)n;

    //创建子进程
    //子进程来写数据
    pid_t id=fork();
    if(id<0)
    {
        perror("fork failed!");
        return 1;
    }
    else if(id==0)
    {
        //关闭读端
        close(pipnum[0]);
        string s1="hello ! I am a precess";
        cout<<"开始写入数据:"<<s1<<endl;

        //写数据
        write(pipnum[1],s1.c_str(),s1.size());
        sleep(5);
        cout<<"数据写入完毕！"<<endl;
        printf("进程退出\n");
        exit(0);
    }
    else
    {
        printf("我是父进程，开始读取数据\n");
        //关闭写端
        close(pipnum[1]);
        //父进程开始读取数据
        char message[100]={0};
        read(pipnum[0],message,1);
        cout<<"Getmessage:"<<message<<endl;

    }
    return 0;
}